package virtualstock.game;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import virtualstock.exception.GameNotFoundException;
import virtualstock.exception.InvalidParameterException;
import virtualstock.request.GameController;
import virtualstock.request.PlayerController;
import virtualstock.request.StockController;
import virtualstock.util.GameDetails;
import virtualstock.util.PlayerDetails;
import virtualstock.util.StockDetails;

/**
 * Servlet implementation class GamePlayServlet
 */
public class GamePlayServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@EJB
	private GameController gameController;
	@EJB
	private StockController stockContoller;
	@EJB
	private PlayerController playerController;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GamePlayServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    	PrintWriter out = response.getWriter();
    	String strGameID = request.getParameter("gameID");
    	Long gameID = new Long(strGameID);
    	//get session
    	HttpSession session = request.getSession();
    	List<GameDetails> gameList = (List<GameDetails>)session.getAttribute("gameList");
    	
    	System.out.println("GamePlayServlet:");
    	System.out.println("!!!!!!!!!!!!!!!!!!!!!!");
    	System.out.println(gameID);
    	System.out.println(gameList);
    	
    	//check if the input gameID in the player's game list
    	boolean flag = false;
    	for(GameDetails gd : gameList){
    		if(gd.getGameID().equals(gameID)){
    			flag = true;
    			break;
    		}
    	}
    	System.out.println("true/false" + flag);
    	if(flag == true){
    		//redirect to game page
    		//collect information about the game
    		//all stock available in this game
    		List<StockDetails> stockList = stockContoller.getAllStocks();
    		//all players involved in this game
    		List<PlayerDetails> playerList = null;
    		GameDetails gameDetail = null;
    		try {
				playerList = (List<PlayerDetails>)playerController.getPlayersOfGame(gameID);
				gameDetail = gameController.getGameDetails(gameID);
    		} catch (InvalidParameterException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch(GameNotFoundException ex){
				ex.printStackTrace();
			}
    		
    		//add to session
    		session.setAttribute("stockList", stockList);
    		
    		request.setAttribute("currentGame", gameDetail);
    		request.setAttribute("playerListInGame", playerList);
    		request.setAttribute("stockList",stockList);
    		request.getRequestDispatcher("game.jsp").forward(request, response);
    	}else{
    		//error
    		response.sendRedirect("error\\errorParticipatedGame.html");
    	}
    
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

}
